package cjzq

import (
	"fmt"
	"log"
	"time"
)

// BaseOperator
type BaseOperator struct {
	cjzqWnd *CjzqWnd
}

// ConnectHtwt
func (baseOperator *BaseOperator) ConnectClient() error {
	mainWnd := NewCjzqWnd()
	err := mainWnd.UpdateWindowHandle()
	if err != nil {
		return fmt.Errorf("mainWnd.UpdateWindowHandle failed, %v", err)
	}
	mainWnd.CloseUnimportancePopWindow()
	baseOperator.cjzqWnd = mainWnd
	return nil
}

// Buy 买入操作
func (baseOperator BaseOperator) BuyNumber(account string, ticker string, number uint64) (float64, error) {
	mainWnd := baseOperator.cjzqWnd
	log.Println(mainWnd)
	mainWnd.workWnd.mscWnd.ShowBuyWnd()
	time.Sleep(time.Second)

	ticker = ticker[2:]

	err := mainWnd.workWnd.buyWnd.FillStockCode(ticker)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.buyWnd.FillStockCode failed, %v", err)
	}

	price, err := mainWnd.workWnd.buyWnd.LoopGetPrice()
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.buyWnd.LoopGetPrice failed, %v", err)
	}

	if price <= 0 {
		return 0, fmt.Errorf("price <= 0")
	}

	count := number // uint64(money / price)
	// count = count / 100
	// count = count * 100
	// if count < 100 {
	// 	return 0, 0, fmt.Errorf("买入数量 < 100 股")
	// }

	log.Printf("买入 %s, 价格: %f, 数量: %d", ticker, price, count)

	ContractNo, err := mainWnd.workWnd.buyWnd.Buy(ticker, price, count)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.buyWnd.Buy failed, %v", err)
	}
	log.Printf("买入已提交, 合同编号ID: %d", ContractNo)

	return price * float64(count), nil
}

// Buy 买入操作
func (baseOperator BaseOperator) BuyMoney(account string, ticker string, money float64) (int, float64, error) {
	mainWnd := baseOperator.cjzqWnd
	log.Println(mainWnd)
	mainWnd.workWnd.mscWnd.ShowBuyWnd()
	time.Sleep(time.Second)

	ticker = ticker[2:]

	err := mainWnd.workWnd.buyWnd.FillStockCode(ticker)
	if err != nil {
		return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.FillStockCode failed, %v", err)
	}

	price, err := mainWnd.workWnd.buyWnd.LoopGetPrice()
	if err != nil {
		return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.LoopGetPrice failed, %v", err)
	}

	if price <= 0 {
		return 0, 0, fmt.Errorf("price <= 0")
	}

	count := uint64(money / price)
	count = count / 100
	count = count * 100
	if count < 100 {
		return 0, 0, fmt.Errorf("买入数量 < 100 股")
	}

	log.Printf("买入 %s, 价格: %f, 数量: %d", ticker, price, count)

	ContractNo, err := mainWnd.workWnd.buyWnd.Buy(ticker, price, count)
	if err != nil {
		return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.Buy failed, %v", err)
	}
	log.Printf("买入已提交, 合同编号ID: %d", ContractNo)

	return int(count), price * float64(count), nil
}

// Sale 卖出操作
func (baseOperator BaseOperator) Sale(account string, ticker string, count uint64) (float64, error) {
	mainWnd := baseOperator.cjzqWnd
	mainWnd.workWnd.mscWnd.ShowSaleWnd()
	time.Sleep(time.Second)

	ticker = ticker[2:]

	err := mainWnd.workWnd.saleWnd.FillStockCode(ticker)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.saleWnd.FillStockCode failed, %v", err)
	}
	price, err := mainWnd.workWnd.saleWnd.LoopGetPrice()
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.saleWnd.LoopGetPrice failed, %v", err)
	}

	if price <= 0 {
		return 0, fmt.Errorf("price <= 0")
	}

	log.Printf("卖出 %s, 价格: %f, 数量: %d", ticker, price, count)

	ContractNo, err := mainWnd.workWnd.saleWnd.Sale(ticker, price, count)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.saleWnd.Sale failed, %v", err)
	}
	log.Printf("卖出已提交, 合同编号ID: %d", ContractNo)

	return price * float64(count), nil
}
